<?php
declare(strict_types=1);

namespace app\model;

use think\Model;

class Kuaidi100Order extends Model
{
    protected $autoWriteTimestamp = true;

    // 订单状态常量
    const STATUS_SUCCESS = 0; // 下单成功
    const STATUS_ACCEPTED = 1; // 已接单
    const STATUS_COLLECTING = 2; // 收件中
    const STATUS_CANCELLED = 99; // 订单已取消
    const STATUS_COLLECTED = 10; // 已取件
    const STATUS_FAILED = 11; // 揽货失败
    const STATUS_RETURNED = 12; // 已退回
    const STATUS_SIGNED = 13; // 已签收
    const STATUS_ABNORMAL = 14; // 异常签收
    const STATUS_SETTLED = 15; // 已结算
    const STATUS_DELIVERING = 101; // 运输中
    const STATUS_PRINTED = 200; // 已出单
    const STATUS_PRINT_FAILED = 201; // 出单失败
    const STATUS_ORDER_FAILED = 610; // 下单失败

    // 支付状态常量
    const PAY_STATUS_HOLD = 0; // 预扣款
    const PAY_STATUS_PAID = 1; // 已支付
    const PAY_STATUS_REFUNDING = 3; // 退款中
    const PAY_STATUS_REFUNDED = 4; // 已退款

    // 关联订单
    public function order()
    {
        return $this->belongsTo(Order::class, 'order_id');
    }

    // 获取状态文字
    public function getStatusTextAttr()
    {
        $status = [
            self::STATUS_SUCCESS => '下单成功',
            self::STATUS_ACCEPTED => '已接单',
            self::STATUS_COLLECTING => '收件中',
            self::STATUS_CANCELLED => '订单已取消',
            self::STATUS_COLLECTED => '已取件',
            self::STATUS_FAILED => '揽货失败',
            self::STATUS_RETURNED => '已退回',
            self::STATUS_SIGNED => '已签收',
            self::STATUS_ABNORMAL => '异常签收',
            self::STATUS_SETTLED => '已结算',
            self::STATUS_DELIVERING => '运输中',
            self::STATUS_PRINTED => '已出单',
            self::STATUS_PRINT_FAILED => '出单失败',
            self::STATUS_ORDER_FAILED => '下单失败'
        ];
        return $status[$this->status] ?? '未知';
    }

    // 获取支付状态文字
    public function getPayStatusTextAttr()
    {
        $status = [
            self::PAY_STATUS_HOLD => '预扣款',
            self::PAY_STATUS_PAID => '已支付',
            self::PAY_STATUS_REFUNDING => '退款中',
            self::PAY_STATUS_REFUNDED => '已退款'
        ];
        return $status[$this->pay_status] ?? '未知';
    }

    // 获取费用明细JSON
    public function getFeeDetailsAttr($value)
    {
        return $value ? json_decode($value, true) : [];
    }

    // 设置费用明细JSON
    public function setFeeDetailsAttr($value)
    {
        return $value ? json_encode($value) : '';
    }
} 